package com.leo.javase.jdbc.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBUtils {
	//数据库连接信息
	private static final String URL = "jdbc:mysql://127.0.0.1:3306/imooc?useUnicode=true&amp;characterEncoding=utf-8";
	private static final String USERNAME = "root";
	private static final String PASSWORD = "12345";
	private static final String DRIVER = "com.mysql.jdbc.Driver";
	
	//数据库连接
	private static Connection connection = null;
	
	/**
	 * 静态代码块
	 * 加载驱动，得到一个连接
	 */
	static{
		try {
			Class.forName(DRIVER);
			connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 工厂方法
	 * @return
	 */
	public static Connection getConnection(){
		return connection;
	}
	
}
